/**
 * @filename:PurReturnServiceImpl Jan 23, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import cn.hutool.core.date.DateTime;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.*;
import com.wonder.app.domain.PurIn;
import com.wonder.app.domain.PurInDetail;
import com.wonder.app.domain.PurReturn;
import com.wonder.app.domain.PurReturnDetail;
import com.wonder.app.mapper.PurInDetailDao;
import com.wonder.app.mapper.PurReturnDao;
import com.wonder.app.mapper.PurReturnDetailDao;
import com.wonder.app.mapper.PurReturnDetailDao;
import com.wonder.app.service.PurInDetailService;
import com.wonder.app.service.PurReturnDetailService;
import com.wonder.app.service.PurReturnService;
import com.wonder.app.request.PurReturnRequest;
import com.wonder.app.service.SyncKingdeeOrderService;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(采购退料服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class PurReturnServiceImpl  extends ServiceImpl<PurReturnDao, PurReturn> implements PurReturnService  {
	/**
	 * Logger
	 */
	private static final Logger _logger = LoggerFactory.getLogger(PurReturnServiceImpl.class);

	@Autowired
	private PurReturnService purReturnService;
	@Autowired
	private PurReturnDao purReturnDao;
	@Autowired
	private PurReturnDetailDao purReturnDetailDao;

	@Autowired
	private PurReturnDetailService purReturnDetailService;

	@Autowired
	private SyncKingdeeOrderService syncKingdeeOrderService;


    /**
     * 验证采购退料信息
     * 
     * @param purReturn
     * @param type
     * @return String
     */
    @Override
    public String validate(PurReturn purReturn,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:采购退料分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<PurReturn>   //分页记录
	 */
	@Override 
	public IPage<PurReturn> selectList(PurReturnRequest request, Page<PurReturn> page){
	    LambdaQueryWrapper<PurReturn> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getId()) {
	       lambdaQueryWrapper.eq(PurReturn::getId, request.getId());
	      }
		if (StringUtils.hasText(request.getBillNo())) {
			lambdaQueryWrapper.like(PurReturn::getBillNo, request.getBillNo());
		}
		if (StringUtils.hasText(request.getSupplierName())) {
			lambdaQueryWrapper.like(PurReturn::getSupplierName, request.getSupplierName());
		}
		if (StringUtils.hasText(request.getAuditUserName())) {
			lambdaQueryWrapper.like(PurReturn::getAuditUserName, request.getAuditUserName());
		}

		if (StringUtils.hasText(request.getWeightNo())) {
			lambdaQueryWrapper.like(PurReturn::getWeightNo, request.getWeightNo());
		}
		if (request.getReturnDate() != null) {
			Date[] fCREATEDATE = request.getReturnDate();
			if (fCREATEDATE.length > 0) {
				// 设置开始时间为当天的零点
				Calendar startCalendar = Calendar.getInstance();
				startCalendar.setTime(fCREATEDATE[0]);
				startCalendar.set(Calendar.HOUR_OF_DAY, 0);
				startCalendar.set(Calendar.MINUTE, 0);
				startCalendar.set(Calendar.SECOND, 0);
				startCalendar.set(Calendar.MILLISECOND, 0);
				Date startDate = startCalendar.getTime();
				lambdaQueryWrapper.ge(PurReturn::getReturnDate, startDate);
			}
			if (fCREATEDATE.length == 2) {
				// 设置结束时间为当天的24点
				Calendar endCalendar = Calendar.getInstance();
				endCalendar.setTime(fCREATEDATE[1]);
				endCalendar.set(Calendar.HOUR_OF_DAY, 23);
				endCalendar.set(Calendar.MINUTE, 59);
				endCalendar.set(Calendar.SECOND, 59);
				endCalendar.set(Calendar.MILLISECOND, 999);
				Date endDate = endCalendar.getTime();
				lambdaQueryWrapper.le(PurReturn::getReturnDate, endDate);
			}
		}

		lambdaQueryWrapper.orderByDesc(PurReturn::getId);	
		Page<PurReturn> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }

	@Override
	public Map<String, Object> getPurReturnList(Map<String, Object> map) {
		int pageIndex = VtuzxUtil.getInt(map,"pageIndex");
		int pageSize = VtuzxUtil.getInt(map,"pageSize");
		String billNo = VtuzxUtil.getString(map,"billNo");//订单号
		List<PurReturn> dataList = purReturnDao.getPurReturnList(pageSize,pageIndex * pageSize,billNo);
		Map<String,Object> data = new HashMap<>();
		data.put("dataList",dataList);
		return data;
	}

	@Override
	public Map<String, Object> getPurReturnDetail(Map<String, Object> map) {
		String billNo = map.get("billNo").toString();
		PurReturn purReturn = purReturnDao.selectOne(new LambdaQueryWrapper<PurReturn>()
				.eq(PurReturn::getBillNo,billNo));
		List<PurReturnDetail> purinList = purReturnDetailDao.selectList(new LambdaQueryWrapper<PurReturnDetail>()
				.eq(PurReturnDetail::getBillNo,billNo));
		Map<String,Object> data = new HashMap<>();
		data.put("data",purReturn);
		data.put("dataList",purinList);
		return data;
	}

	@Override
	public Boolean auditPurReturn(Long user_id, Map<String, Object> map,String userName) throws Exception{
		String billNo = VtuzxUtil.getString(map,"billNo");
		PurReturn purReturn = purReturnDao.selectOne(new LambdaQueryWrapper<PurReturn>()
		.eq(PurReturn::getBillNo,billNo));
		List<PurReturnDetail> detailList = purReturnDetailDao.selectList(new LambdaQueryWrapper<PurReturnDetail>()
				.eq(PurReturnDetail::getBillNo,billNo));
		purReturn.setStatus("audit");
		purReturn.setAuditTime(new DateTime());
		purReturn.setAuditUserName(userName);

		try {
			syncKingdeeOrderService.syncPurReturn(user_id,userName,purReturn,detailList);
		}catch (Exception e){
			throw new RuntimeException(e.getMessage());
		}
		boolean res = purReturnService.saveOrUpdate(purReturn);
		return res;
	}



	/**
	 * receive详情
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> getPurRetDetail(Map<String, Object> map) throws Exception{
		String billNo = map.get("billNo").toString();
		PurReturn purreturnform = purReturnDao.selectOne(new LambdaQueryWrapper<PurReturn>()
				.eq(PurReturn::getBillNo,billNo));
		if (null == purreturnform){
			throw new RuntimeException("单号错误请重试");
		}
		List<PurReturnDetail> purreturnList = purReturnDetailDao.selectList(new LambdaQueryWrapper<PurReturnDetail>()
				.eq(PurReturnDetail::getBillNo,billNo));
		Map<String,Object> data = new HashMap<>();
		data.put("data",purreturnform);
		data.put("dataList",purreturnList);
		return data;
	}


}